/**
 * #########################################################################################################################
 * #Create Game developed by Evaldo Sávio (evaldosavioaraujo@gmail.com) and Gabriel Gonzalez (gabrielgonzalez.95@gmail.com)#
 * #########################################################################################################################
 * #Create Game desenvolvido por Evaldo Sávio(evaldosavioaraujo@gmail.com) e Gabriel Gonzalez(gabrielgonzalez.95@gmail.com)#
 * #							Use-o livremente, mas mantenha e os créditos, por favor.                               #
 * #########################################################################################################################
 */

package br.phb.cgame.sprites;

import java.util.HashMap;
import java.util.Map;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;
import br.phb.cgame.views.SpriteView;

/**
 * Esta classe consiste em um simples personagem de ida e volta, podendo ser
 * utilizado para objetos de cenário.
 * 
 * @author Evaldo Sávio
 * @author Gabriel Gonzalez
 */
public class BasicSprite {

	private Bitmap frames;
	private int firstFrame;
	private int lastFrame;
	private int cursorFrame;
	private int width, height;
	private int columns;
	private Context context;
	private SpriteView spriteView;

	public int positionX, positionY;
	public Map<String, int[]> moviments;

	/**
	 * @param frames
	 * @date_modify Feb 16, 2013
	 * @author evaldosavio
	 */
	public BasicSprite( Context context, int spriteId, int lines, int columns, int positionX, int positionY ) {
		Resources res = context.getResources();
		Bitmap frames = BitmapFactory.decodeResource( res, spriteId );
		this.frames = frames;
		this.lastFrame = ( columns * lines ) - 1;
		this.firstFrame = 1;
		this.cursorFrame = 1;

		this.columns = columns;
		this.width = this.frames.getWidth() / columns;
		this.height = this.frames.getHeight() / lines;
		this.positionX = positionX;
		this.positionY = positionY;
		this.context = context;

		this.spriteView = new SpriteView( this.context, this );
		this.moviments = new HashMap<String, int[]>();
	}

	public Rect getFrameRect() {
		int srcX = ( cursorFrame % columns ) * width;
		int srcY = ( ( (int) cursorFrame ) / columns ) * height;
		return new Rect( srcX, srcY, srcX + width, srcY + height );
	}

	public int getFirstFrame() {
		return firstFrame;
	}

	public void setFirstFrame( int firstFrame ) {
		this.firstFrame = firstFrame;
	}

	public int getLastFrame() {
		return lastFrame;
	}

	public void setLastFrame( int lastFrame ) {
		this.lastFrame = lastFrame;
	}

	public int getCursorFrame() {
		return cursorFrame;
	}

	public void setCursorFrame( int cursorFrame ) {
		this.cursorFrame = cursorFrame;
	}

	public int getWidth() {
		return width;
	}

	public int getHeight() {
		return height;
	}

	public Bitmap getFrames() {
		return frames;
	}

	protected void animate() {

	}

	public void onClick() {
		// TODO
	}

	public void onTouch() {
		// TODO
	}

	public void onDie() {
		// TODO
	}

	public void onCollide() {
		// TODO
	}

	public View getView() {
		return this.spriteView;
	}

	int i = 0;
	boolean animating = true;

	public void animate( String moviment ) {
		final int[] frames = this.moviments.get( moviment );
		Log.d( "LS", "--------->" + i );
		cursorFrame = frames[i];
		this.spriteView.invalidate();
		i++;
		i = i < frames.length ? i : 0;
	}
}
